import React from "react";
import { Form, Select, Input, Space, Button } from "antd";
import { apiGetCouponInfoList} from '@/request/coupons'
import { apiGetByCouponCentre } from '@/request/couponManagement'
import { cosh } from "core-js/core/number";

const { TextArea } = Input;

class Banned extends React.Component {
  state = {
    options:[],
    loading:false
  }
  async componentDidMount() {
    await this.getData()
  }
  getData= async ()=>{
    let res = await apiGetCouponInfoList({couponStatus:'Y',deleted:0})
    if(res.code == 0){
      res.data.map((item,i)=>{
        return this.setState({
          options:[...this.state.options,{label:item.couponTitle,value:item.couponId}]
        })
      })
    }
  }
  finishClick = async value => {
    this.setState({ loading: true })
    value = { ...value, memberId: this.props.memberId, sourceId:2 }
    let res = await apiGetByCouponCentre({
      ...value,
    })
    if (res.code === 0 && res.data) {
      if (res.data.success){
        React.$message({
          type: 'info',
          content: '发放成功'
        })
        this.props.reload()
        this.props.dialogClose()
      }else {
        React.$message({
          type: 'info',
          content: res.data.msg
        })
        this.setState({ loading: false })
      }
    }else{
      this.setState({ loading: false })
    }

    // let yhq = await React.$post(`/admin/memberCoupon/add`,value)
    // // console.log(yhq)
    // if(yhq.code == 0){
    //   React.$message({
    //     type: 'info',
    //     content: '发放成功'
    //   })
    //   this.props.reload()
    //   this.props.dialogClose()
    // }else{
    //   this.setState({ loading: false })
    //   // this.props.dialogClose()
    // }
  }
  onChange() {
    
  }
  onSearch() {

  }
  render() {
    const {options,loading} = this.state
    return (
      <div>
        <Form onFinish={this.finishClick} labelCol={{ span: 5 }} labelAlign="right">
          <Form.Item label="优惠券" rules={[{ required: true }]} name={'couponId'}>
            <Select 
              showSearch
              optionFilterProp="children"
              onChange={()=> this.onChange() }
              onSearch={()=> this.onSearch() }
              filterOption={(input, option) =>
                (option?.label ?? '').toLowerCase().includes(input.toLowerCase())
              } options={options} />
          </Form.Item>
          {/* <Form.Item label="备注" name="bannedReason" rules={[{ required: true }]}>
            <TextArea
              allowClear
              autoSize={{ minRows: 6, maxRows: 6 }}
              showCount
              maxLength={50}
            />
          </Form.Item> */}
          <Form.Item>
            <div
              style={{
                display: "flex",
                justifyContent: "flex-end",
                marginBottom: "-20px",
                marginTop: 20,
              }}
            >
              <Space>
                <Button onClick={this.props.dialogClose}>取消</Button>
                <Button type="primary" htmlType="submit" loading={loading}>保存</Button>
              </Space>
            </div>
          </Form.Item>
        </Form>
      </div>
    );
  }
}

export default Banned;
